package com.xqh.commom.taglib;

import com.xqh.commom.SystemUserUtils;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * freemarker自定义 权限标签
 */
@Component("permissionDirective")
public class PermissionDirective implements TemplateDirectiveModel {
    @Override
    public void execute(Environment env, Map map, TemplateModel[] model, TemplateDirectiveBody body) throws TemplateException, IOException {
        Object permissionObj = map.get("name");
        String permission = (permissionObj == null ? "" : permissionObj.toString());
        if(SystemUserUtils.hasPermission(permission)){
            if(body != null){
                body.render(env.getOut());
            }
        }else{
            env.getOut().write("");
        }
    }
}
